﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using BrokerBase.Lib.Query;
using SaleLibrary.Score;
using BrokerBase.Lib.Core;

namespace Broker.Mgr.SaleView
{
    /// <summary>
    /// Interaction logic for ScoreRuleListPage.xaml
    /// </summary>
    public partial class ScorePolicyListPage
    {
        private const int PAGE_SIZE = 15;

        private PagingQuery pageingQuery;

        private string hqlBase = "from ScorePolicy as s ";//默认hql


        public ScorePolicyListPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ScoreRuleListPage_Loaded);
        }

        void ScoreRuleListPage_Loaded(object sender, RoutedEventArgs e)
        {
            
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            IScorePolicy policy = new ScorePolicy();
            policy.CreateTime = DateTime.Now;

            ScorePolicyForm frm = new ScorePolicyForm(policy);

            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.Owner = AppGlobal.MinWin;
            frm.ShowDialog();

            

        }

        private void btnView_Click(object sender, RoutedEventArgs e)
        {
            View();
        }

        

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            IScorePolicy sr = gvScoreRule.SelectedItem as IScorePolicy;

            if (sr != null)
            {
                if (MessageBox.Show(string.Format("是否要删除积分政策:{0}", sr.Name), "提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    try
                    {
                        IBaseService<ScorePolicy> svr = new BaseService<ScorePolicy>(AppGlobal.HbmSessionFactory.GetCurrentSession());

                        svr.Delete(sr as ScorePolicy);
                        Query();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private void View()
        {
            IScorePolicy policy = gvScoreRule.SelectedItem as IScorePolicy;

            if (policy != null)
            {
                ScorePolicyForm frm = new ScorePolicyForm(policy);

                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.Owner = AppGlobal.MinWin;
                frm.ShowDialog();
            }

        }

        private void BuildQuery()
        {
            pageingQuery = new PagingQuery();

            string where = "where 1=1";

            if (!string.IsNullOrEmpty(txtName.Text))
            {
                where += string.Format(" and s.Name Like '%{0}%'", txtName.Text.Trim());
            }

            pageingQuery.HQL = hqlBase + where ;
            pageingQuery.TotalHQL = "select count(*) " + hqlBase + where;
        }

        protected void Query()
        {
            try
            {
                busyRoot.IsBusy = true;
                busyRoot.BusyContent = "正在加载数据...";

                IBaseService<ScorePolicy> svr = new BaseService<ScorePolicy>(AppGlobal.HbmSessionFactory.GetCurrentSession());

                long total = 0;
                IList<ScorePolicy> ls = svr.GetListByHQLWithPaging(pageingQuery.HQL, pageingQuery.TotalHQL, pageingQuery.FirstResult, PAGE_SIZE, out total);

                int t3 = Environment.TickCount;


                dpList.ItemCount = (int)total;
                gvScoreRule.ItemsSource = ls;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                busyRoot.IsBusy = false;
            }

        }

        private void dpList_PageIndexChanged(object sender, PageIndexChangedEventArgs e)
        {
            if (pageingQuery != null)
            {
                pageingQuery.FirstResult = e.NewPageIndex * PAGE_SIZE;
                Query();
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            BuildQuery();
            Query();
        }

        private void gvScoreRule_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            View();
        }



    }
}
